import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.geotools.data.simple.*;
import org.opengis.feature.*;
import org.opengis.feature.simple.*;
import org.opengis.feature.type.*;

import utilityTools.ExcelUtility;

/**
 * 读取shp数据，并将属性写入Excel中
 * 通过POI导出数据到Excel
 */
public class Shp2Excel {
    String mcPath_Shp;
    String mcPath_Excel;

    public Shp2Excel(String pPath_Shp, String pPath_Excel) {
        this.mcPath_Shp = pPath_Shp;
        this.mcPath_Excel = pPath_Excel;
    }

    public void exportAttr2Excel() throws Exception {
        int lpCounter = 0;
        List<List<Object>> listRows = new ArrayList<List<Object>>();//存储整个数据表
        //获取shp原始数据
        SimpleFeatureCollection lpFeaColl_Source = ShpfileReader.getShpfileData(mcPath_Shp);
        //获取shp的属性表结构
        List<ShpFieldInfo> fieldInfoList = new ArrayList<ShpFieldInfo>();
        List<AttributeDescriptor> attrList = lpFeaColl_Source.getSchema().getAttributeDescriptors();
        for (AttributeDescriptor perAttr : attrList) {
            ShpFieldInfo perField = new ShpFieldInfo();
            perField.setFieldName(perAttr.getLocalName());
            perField.setFieldType(perAttr.getType().getBinding());
            fieldInfoList.add(perField);
        }
        //存储列标题
        List<Object> listColumnName = new ArrayList<Object>();//存储每一行的数据集
        for (ShpFieldInfo perField : fieldInfoList) {
            listColumnName.add(perField.getFieldName());
        }
        listRows.add(listColumnName);
        //开始构造属性表结果集
        SimpleFeatureIterator iteratorSourceShp = lpFeaColl_Source.features();
        while (iteratorSourceShp.hasNext()) {
            lpCounter++;
            List<Object> listColumnValue = new ArrayList<Object>();//存储每一行的数据集
            System.out.println("开始读取第" + lpCounter + "个要素");
            SimpleFeature ltFea_Shp = iteratorSourceShp.next();
            //下面这种方式是按照原有顺序获取属性，如果需要单独获取则使用'getAttribute("列名称")'方式
            Iterator<Property> lpIterator_Property = ltFea_Shp.getProperties().iterator();
            while (lpIterator_Property.hasNext()) {
                Property ltProperty = lpIterator_Property.next();
                //跳过几何字段
                if (ltProperty.getName().toString().equalsIgnoreCase("the_geom")) {
                    continue;
                }
                //完全按照原有的顺序
                listColumnValue.add(ltProperty.getValue());
            }
            //把当前行的数据添加到结果集中
            listRows.add(listColumnValue);
            if (lpCounter > 10) {
                break;
            }
        }
        iteratorSourceShp.close();
        //导出结果集到Excel中
        String lpPath_Excel = "E:\\9.Tests\\shpAttr2Excel.xlsx";
        try {
//            ExcelUtility excelOpra = new ExcelUtility(lpPath_Excel);
            ExcelUtility excelOpra = new ExcelUtility(mcPath_Excel);
            //两个索引都是从0开始，如果已经存在表头，那么跳过表头行即可
            excelOpra.writeDatas(0, 1, listRows);
        } catch (FileNotFoundException error1) {
            error1.printStackTrace();
        } catch (Exception error) {
            error.printStackTrace();
        }
        System.out.println("数据导出结束,End");
    }
}
